package algorithms.hint;
import java.util.*;
import configuration.Configuration;
import simulator.Manager;
import simulator.Client;
import storage.Disk;
/**
* @author Nodirjon Siddikov
*/
public class HintManager extends Manager
{
//global cache information
//key - client ID
//value - list of facts about each client
private HashMap<Integer, ArrayList<Fact>> _facts; //facts about locations of MASTER COPY in each client
private HintClient[] _clients;
//constructor
public HintManager(int id, Disk disk)
{
super(id, disk);
_facts = new HashMap<Integer, ArrayList<Fact>>();
_clients = new HintClient[Configuration.noOfClients];
}
//get the list of clients
public HintClient[] getAllClients()
{
return _clients;
}
//set the clients for the manager
public void setClients(Client[] clients)
{
//get the list of clients
for(int i = 0; i < clients.length; i++)
{
_clients[i] = (HintClient)clients[i];
}
}
//get client reference
public HintClient getClient(int id)
{
for(HintClient c: _clients)
{
if(c.getID() == id)
{
return c;
}
}
//returns null if there is no a Client with such ID
return null;
}
//add fact
public void addFact(int clientID, int diskBlockID)
{
if(_facts.get(clientID) == null)
{
//create new list for the src client
ArrayList<Fact> factsList = new ArrayList<Fact>();
_facts.put(clientID, factsList);
}
//add new fact about client
_facts.get(clientID).add(new Fact(diskBlockID));
}
//remove fact
public void removeFact(int clientID, int diskBlockID)
{
//index of the arraylist element to be removed
for(Fact f: _facts.get(clientID))
{
if(f.getDiskBlockID() == diskBlockID)
{
//manager removes the relevant disk block
_facts.get(clientID).remove(f);
return;
}
}
}
//get the ID of the client that is caching the disk block
public Hint getHint(int diskBlockID)
{
for(HintClient c: _clients)
{
ArrayList<Fact> factsList = _facts.get(c.getID());
if(factsList != null)
{
for(Fact f: factsList)
{
if(f.getDiskBlockID() == diskBlockID)
{
return new Hint(diskBlockID, c.getID());
}
}
}
}
return null;
}
//calculate the coop cache size
public int getGlobalCacheSize()
{
int mgrCoopCacheSize, ttlCoopCacheSize = 0;
int factCount = 0;
//calculate manager coop cache size
for(int clientID: _facts.keySet())
{
factCount += _facts.get(clientID).size();
}
mgrCoopCacheSize = factCount * Configuration.hintSize; //sizes of hints and facts are assumed to be same
//calculate client coop cache size
for(HintClient c: _clients)
{
ttlCoopCacheSize += c.getClientCoopCacheSize();
}
//return total coop cache size
return mgrCoopCacheSize + ttlCoopCacheSize;
}
}